<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>权限管理</title>
    <link href="../../static/component/pear/css/pear.css" rel="stylesheet" th:href="@{/static/component/pear/css/pear.css}"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
        	<div class="layui-form-item">
        		<div class="layui-form-item layui-inline">
        			<label class="layui-form-label">菜单名称</label>
        			<div class="layui-input-inline">
        				<input type="text" name="title" placeholder="" class="layui-input">
        			</div>
        		</div>
        		<div class="layui-form-item layui-inline">
        			<button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
        				<i class="layui-icon layui-icon-search"></i>
        				查询
        			</button>
        			<button type="reset" class="pear-btn pear-btn-md">
        				<i class="layui-icon layui-icon-refresh"></i>
        				重置
        			</button>
        		</div>
        	</div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="power-table" lay-filter="power-table"></table>
    </div>
</div>

<script type="text/html" id="power-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
	<button class="pear-btn pear-btn-success pear-btn-md" lay-event="expandAll">
	    <i class="layui-icon layui-icon-spread-left"></i>
	    展开
	</button>
	<button class="pear-btn pear-btn-success pear-btn-md" lay-event="foldAll">
	    <i class="layui-icon layui-icon-shrink-right"></i>
	    折叠
	</button>
</script>

<script type="text/html" id="power-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
</script>

<script type="text/html" id="power-type">
    {{#if (d.type == '0') { }}
    <span>目录</span>
    {{# }else if(d.type == '1'){ }}
    <span>菜单</span>
    {{# }else if(d.type == '2'){ }}
    <span>按钮</span>
    {{# } }}
</script>

<script type="text/html" id="power-enable">
    <input type="checkbox" name="enable" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="power-enable"  {{ d.enable== 1 ? 'checked' : '' }} />
</script>

<script type="text/html" id="icon">
    <i class="layui-icon {{d.icon}}"></i>
</script>

<script src="../../static/component/layui/layui.js" th:src="@{/static/component/layui/layui.js}"></script>
<script src="../../static/component/pear/pear.js" th:src="@{/static/component/pear/pear.js}"></script>
<script>
    layui.use(['table','form','jquery','treetable','dtree'],function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let treetable = layui.treetable;


        let render = function(){
            treetable.render({
                treeColIndex: 1,
                treeSpid: 0,
                treeIdName: 'parent_id',
                treePidName: 'parent_id',
                skin:'line',
                treeDefaultClose: true,
                toolbar:'#power-toolbar',
                elem: '#power-table',
                url: '/system/power/powerList',
                page: false,
                cols: [
                    [
                    {type: 'checkbox'},
                    {field: 'title', minWidth: 200, title: '权限名称'},
                    {field: 'icon', title: '图标',templet:'#icon'},
                    {field: 'type', title: '权限类型',templet:'#power-type'},
                    {field: 'power_code', title: '权限标识', width: 150},
                    {field: 'enable', title: '是否可用',templet:'#power-enable'},
                    {field: 'sort', title: '排序'},
                    {title: '操作',templet: '#power-bar', width: 150, align: 'center'}
                    ]
                ]
            });
        }

        render();

        table.on('tool(power-table)',function(obj){
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        })


        table.on('toolbar(power-table)', function(obj){
            if(obj.event === 'add'){
                window.add();
            } else if(obj.event === 'refresh'){
                window.refresh();
            } else if(obj.event === 'batchRemove'){
                window.batchRemove(obj);
            }  else if(obj.event === 'expandAll'){
				 treetable.expandAll("#power-table");
			} else if(obj.event === 'foldAll'){
				 treetable.foldAll("#power-table");
			}
        });

		form.on('switch(power-enable)', function(obj) {
            // console.log(obj.elem.checked)
			var enable = 0;
			if (obj.elem.checked){
				enable = 1
			}
			$.ajax({
				type:'post',
				url:"/system/power/enableupdate",
				dataType:'json',
				data:JSON.stringify({"id":this.value,"enable":enable}),
				contentType:'application/json',
				success:function (data) {

					if (data.code === 500){
						layer.tips(data.msg, obj.othis);

					}
					if (data.code === 0){
						layer.tips(data.msg, obj.othis);
					}

				},
				error:function () {
					layer.alert("网络错误")
				}
			});
		});
        window.add = function(){
            //获取当前页面的高度
            var height = $(document.body).height();
            //获取当前页面的宽度
            var width = $(document.body).width();
            layer.open({
                type: 2,
                title: '添加菜单',
                shade: 0.1,
                area:[Number(width/2)+Number(width/6)+"px",Number(height*0.8)+"px"],
                maxmin:true,
                content: '/system/power/add.html'
            });
        }

        window.edit = function(obj){
            // console.log(obj)
            //获取当前页面的高度
            var height = $(document.body).height();
            //获取当前页面的宽度
            var width = $(document.body).width();
            layer.open({
                type: 2,
                title: '修改菜单',
                shade: 0.1,
                area:[Number(width/2)+Number(width/6)+"px",Number(height*0.8)+"px"],
                content: '/system/power/edit.html?id='+obj.data['id']
            });
        }
        window.remove = function(obj){
            layer.confirm('确定要删除该菜单', {icon: 3, title:'提示'}, function(index){
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/system/power/remove",
                    dataType:'json',
                    type:'post',
                    data:JSON.stringify({"id":obj.data['id']}),
                    contentType:'application/json',
                    success:function(result){
                        layer.close(loading);
                        if(result.code ===0){
                            layer.msg(result.msg,{icon:1,time:1000},function(){
                                treetable.reload("#power-table");
                            });
                        }else{
                            layer.msg(result.msg,{icon:2,time:1000});
                        }
                    }
                })
            });
        }
		
		window.batchRemove = function(obj) {
			let data = table.checkStatus(obj.config.id).data;
            // console.log(data)
			if (data.length === 0) {
				layer.msg("未选中数据", {
					icon: 3,
					time: 1000
				});
				return false;
			}
			let ids = "";
			for (let i = 0; i < data.length; i++) {
				ids += data[i].id + ",";
			}
			ids = ids.substr(0, ids.length - 1);
			layer.confirm('确定要删除这些菜单', {
				icon: 3,
				title: '提示'
			}, function(index) {
				layer.close(index);
				let loading = layer.load();
				$.ajax({
                    url: "/system/power/remove",
                    dataType: 'json',
                    type: 'post',
                    data:JSON.stringify({"id":ids}),
                    contentType:'application/json',
                    success:function(result){
                        layer.close(loading);
                        if(result.code ===0){
                            layer.msg(result.msg,{icon:1,time:1000},function(){
                                treetable.reload("#power-table");
                            });
                        }else{
                            layer.msg(result.msg,{icon:2,time:1000});
                        }
                    }
				})
			});
		}
    })
</script>
</body>
</html>